package com.ywl.study.transaction.util;


import com.ywl.study.transaction.transaction.DistirctTransactionManager;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

public class HttpClientUtil {

    static private HttpClientUtil instance=new HttpClientUtil();

    public static HttpClientUtil getInstance(){
        return instance;
    }

    public  HttpResponse url(String hostname) throws IOException, InterruptedException {
        HttpClient client = HttpClient.newBuilder()
                .connectTimeout(Duration.ofMillis(5000))
                .followRedirects(HttpClient.Redirect.NORMAL)
                .build();

        //2.set read timeout
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:8082/"))
                .header("groupId", DistirctTransactionManager.getCurrentGroupId())
                .timeout(Duration.ofMillis(5009))
                .build();

        HttpResponse<String> response =
                client.send(request, HttpResponse.BodyHandlers.ofString());
        return response;
    }


}
